package com.example.dell1.helloworld;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private TextView edit_1;
    private TextView edit_2;
    private TableLayout tableLayout;

    protected void onCreate(Bundle savedInstanceState) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog().build());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);
        edit_1 = findViewById(R.id.edit_1);
        edit_2 = findViewById(R.id.edit_2);
        tableLayout = findViewById(R.id.table);
        tableLayout.setStretchAllColumns(true);


        //这里是导入操作
       /* data_interact my_get = null;
        String []name=null,id=null; //name为用户名，id为学号
        try {
            String str = my_get.getinfo();
            int num = 0;
            int startposition = 0;
            while(startposition<str.length()){
                int findresult = str.indexOf("{", startposition);
                if (findresult>0) {
                    startposition = findresult +1;
                    num++;
                }
                else break;
            }
            name = new String[num];
            id = new String[num];
            String []s = str.split(",");
            int j = 0,k = 0;
            for (int i = 0; i < 2*num; i++) {
                // System.out.println(i);
                String[] b = s[i].split(":");
                int x, y;
                x = b[1].indexOf("\"");
                y = b[1].lastIndexOf("\"");
                if (i % 2 == 0) {
                    name[i / 2] = b[1].substring(x + 1, y);
                } else {
                    id[i / 2] = b[1].substring(x + 1, y);
                }

            }

        } catch (IOException e) {
            //System.out.println("Error!");
            e.printStackTrace();
        }*/

        data_interact my_get = null;
        String []name=null,id=null;
        int num = 0;
        String str = null;
        try {
            str = my_get.getinfo();
            int startposition = 0;
            while(startposition<str.length()){
                int findresult = str.indexOf("{", startposition);
                if (findresult>0) {
                    startposition = findresult +1;
                    num++;
                }
                else break;
            }
            name = new String[num];
            id = new String[num];
            String []s = str.split(",");
            int j = 0,k = 0;
            for (int i = 0; i < 2*num; i++) {
                // System.out.println(i);
                String[] b = s[i].split(":");
                int x, y;
                x = b[1].indexOf("\"");
                y = b[1].lastIndexOf("\"");
                if (i % 2 == 0) {
                    name[i / 2] = b[1].substring(x + 1, y);
                } else {
                    id[i / 2] = b[1].substring(x + 1, y);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        //这里是导入操作
        for(int i = 0; i< num; i++) {
            TableRow tableRow = new TableRow(MainActivity.this);
            tableRow.setBackgroundColor(Color.rgb(222, 220, 210));

            final TextView textView_1 = new TextView(MainActivity.this);
            textView_1.setBackgroundResource(R.drawable.shapee);
            CharSequence temp1 = id[i];
            textView_1.setText(temp1);
            tableRow.addView(textView_1);

            final TextView textView_2 = new TextView(MainActivity.this);
            textView_2.setBackgroundResource(R.drawable.shapee);
            CharSequence temp2 = name[i];
            textView_2.setText(temp2);
            tableRow.addView(textView_2);

            Button delete_button = new Button(MainActivity.this);
            delete_button.setText("删除");
            delete_button.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            TableRow tableRow1 = (TableRow) view.getParent();
                            tableLayout.removeView(tableRow1);
                            //这里是删除操作
                            data_interact my_get = null;
                            try {
                                my_get.delete(textView_2.getText().toString(),textView_1.getText().toString());  // name为学生姓名，id为学生学号，姓名和学号必须对应。
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
            );
            tableRow.addView(delete_button);
            tableLayout.addView(tableRow);
        }


        class ButtonListener implements View.OnClickListener {

            @SuppressLint("ClickableViewAccessibility")
            public void onClick(View v) {
                final TableRow tableRow = new TableRow(MainActivity.this);
                tableRow.setBackgroundColor(Color.rgb(222, 220, 210));

                final TextView textView_1 = new TextView(MainActivity.this);
                textView_1.setBackgroundResource(R.drawable.shapee);
                textView_1.setText(edit_1.getText());
                tableRow.addView(textView_1);

                final TextView textView_2 = new TextView(MainActivity.this);
                textView_2.setBackgroundResource(R.drawable.shapee);
                textView_2.setText(edit_2.getText());
                tableRow.addView(textView_2);

                Button delete_button = new Button(MainActivity.this);
                delete_button.setText("删除");
                delete_button.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                TableRow tableRow1 = (TableRow) view.getParent();
                                tableLayout.removeView(tableRow1);
                                //这里是删除操作
                                data_interact my_get = null;
                                try {
                                    my_get.delete(textView_2.getText().toString(),textView_1.getText().toString());  // name为学生姓名，id为学生学号，姓名和学号必须对应。
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                );
                tableRow.addView(delete_button);

                //这里是添加操作
                data_interact my_get = null;
                try{
                    my_get.insert(textView_2.getText().toString(), textView_1.getText().toString());
                }catch (IOException e){
                    e.printStackTrace();
                }
                //my_get.insert(name.id);   name为学生姓名，不宜太长，id为学生学号
                tableLayout.addView(tableRow);
            }
        }

        ButtonListener buttonListener = new ButtonListener();
        button.setOnClickListener(buttonListener);

    }
}
